package com.lanou.beautifulwardrobe.Home.Adapter;

import android.graphics.Bitmap;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.android.volley.Response;
import com.lanou.beautifulwardrobe.R;
import com.lanou.beautifulwardrobe.contants.Contants;
import com.lanou.beautifulwardrobe.contants.MyImageView;
import com.lanou.beautifulwardrobe.contants.TimeManager;
import com.lanou.beautifulwardrobe.contants.VolleySingleton;
import com.lanou.beautifulwardrobe.entify.HotlistLike;
import com.lanou.beautifulwardrobe.entify.HotlistProduct;
import com.lanou.beautifulwardrobe.entify.WaterfallCategory;
import com.lanou.beautifulwardrobe.listener_interface.MyHotlistItemClickListener;
import com.lanou.beautifulwardrobe.listener_interface.MyItemClickListener;

import java.util.List;

/**
 * Created by dllo on 15/10/22.
 */
public class HotlistRecyclerViewAdapter extends RecyclerView.Adapter<HotlistRecyclerViewAdapter.HotlistViewHolder> {
    private VolleySingleton volleySingleton = VolleySingleton.getInstance();
    private List<HotlistLike> datas;
    private MyHotlistItemClickListener listener;
    public void setOnMyHotlistItemClickListener(MyHotlistItemClickListener listener){
        this.listener=listener;
    }


    public void addData(List<HotlistLike> likes) {
        datas = likes;
        notifyDataSetChanged();
    }

    @Override
    public HotlistViewHolder onCreateViewHolder(ViewGroup view, int viewType) {
        View v = LayoutInflater.from(view.getContext()).inflate(R.layout.hotlist_item, null);
        return new HotlistViewHolder(v);
    }

    @Override
    public void onBindViewHolder(HotlistViewHolder holder, int position) {
        if (datas != null && datas.size() > 0) {
            holder.position=position;
            HotlistLike like=datas.get(position);
            getBitmap(like.getUser_image_url(), holder.hotlistUserPic);
            holder.hotlistUserName.setText(like.getUser_name());
            holder.hotlistTime.setText(TimeManager.timeStamp2Date(like.getCreated_time(), "MM月dd日 HH:mm"));
            HotlistProduct product=like.getProduct();
            getBitmap(product.getTaobao_pic_url(), holder.hotlistProductPic);
            holder.hotlistProductPrice.setText(product.getMoney_symbol() + product.getTaobao_selling_price());
            holder.hotlistVolume.setText(product.getTaobao_volume());
        }

    }

    @Override
    public int getItemCount() {
        return datas != null && datas.size() > 0 ? datas.size() : 0;
    }


    class HotlistViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
        private LinearLayout hotlistLayout;
        private MyImageView hotlistUserPic;
        private ImageView hotlistProductPic;
        private TextView hotlistUserName, hotlistTime, hotlistProductPrice, hotlistVolume;

        private int position;

        public HotlistViewHolder(View view) {
            super(view);
            hotlistLayout= (LinearLayout) view.findViewById(R.id.hotlistLayout);
            hotlistUserPic = (MyImageView) view.findViewById(R.id.hotlistUserPic);
            hotlistProductPic = (ImageView) view.findViewById(R.id.hotlistProductPic);
            hotlistUserName = (TextView) view.findViewById(R.id.hotlistUserName);
            hotlistTime = (TextView) view.findViewById(R.id.hotlistTime);
            hotlistProductPrice = (TextView) view.findViewById(R.id.hotlistProductPrice);
            hotlistVolume = (TextView) view.findViewById(R.id.hotlistVolume);
            hotlistLayout.setOnClickListener(this);

        }

        @Override
        public void onClick(View v) {
            if (datas != null && datas.size() > 0) {
                String id=datas.get(position).getProduct().getTaobao_num_iid();
                if (listener != null) {
                    listener.onHotlistItemClick(id);
                }
            }
        }
    }

    public void getBitmap(String url, final ImageView imageView) {
        volleySingleton.getImageResuest(url, new Response.Listener<Bitmap>() {
            @Override
            public void onResponse(Bitmap response) {
                imageView.setImageBitmap(response);
            }
        });
    }
}
